package com.jn.mvvm2.service;

import android.util.Log;

import com.jn.mvvm2.entity.Pet;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PetRepository {
    private static final String TAG = "PetRepository";

    public void getPets(String url, String type, String page, String num,String name, PetCallback callback) {
        ApiService apiService = RetrofitClient.getInstance(url).create(ApiService.class);
        apiService.getPets("edca853fca917a24e47da1733b01a538", type, page, num,name).enqueue(new Callback<Pet>() {
            @Override
            public void onResponse(Call<Pet> call, Response<Pet> response) {
                if (response.isSuccessful() && response.body() != null) {
                    Log.d(TAG, "获取数据成功: " + response.body().getResult().getList().size());
                    callback.onSuccess(response.body().getResult().getList());
                } else {
                    callback.onFailure("未能获取数据");
                    Log.e(TAG, "获取数据失败: " + response.message());
                }
            }

            @Override
            public void onFailure(Call<Pet> call, Throwable t) {
                callback.onFailure(t.getMessage());
                Log.e(TAG, "请求失败: " + t.getMessage());
            }
        });
    }

    public void searchPets(String url, String query, PetCallback callback) {
        ApiService apiService = RetrofitClient.getInstance(url).create(ApiService.class);
        apiService.searchPets("edca853fca917a24e47da1733b01a538", query).enqueue(new Callback<Pet>() {
            @Override
            public void onResponse(Call<Pet> call, Response<Pet> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body().getResult().getList());
                } else {
                    callback.onFailure("搜索数据失败");
                }
            }

            @Override
            public void onFailure(Call<Pet> call, Throwable t) {
                callback.onFailure(t.getMessage());
            }
        });
    }

    public interface PetCallback {
        void onSuccess(List<Pet.PetDetails> pets);
        void onFailure(String errorMessage);
    }
}
